<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 引入 lib 目录下的 bootstrap 样式表 -->
  <link rel="stylesheet" href="./lib/bootstrap-v4.6.0.css">
  <style>
    :root {
      font-size: 15px;
    }

    body {
      padding-top: 15px;
    }
  </style>
</head>

<body>

  <!-- 栅格系统 -->
  <div class="container-fluid">
    <!-- 栅格系统中的一行 -->
    <div class="row">
      <!-- 左侧的表格，占了 8 列 -->
      <div class="col-sm-8">
        <table class="table table-bordered table-striped table-dark table-hover text-center">
          <thead>
            <!-- 表头行 -->
            <tr>
              <th scope="col">Id</th>
              <th scope="col">书名</th>
              <th scope="col">作者</th>
              <th scope="col">出版社</th>
              <th scope="col">操作</th>
            </tr>
          </thead>
          <tbody>
            <!-- 表格中的每一行 -->
            <tr>
              <th scope="row">xxx</th>
              <td>xxx</td>
              <td>xxx</td>
              <td>xxx</td>
              <td>
                <button type="button" class="btn btn-link btn-sm">删除</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>

      <!-- 右侧的添加区域，占了 4 列 -->
      <div class="col-sm-4">
        <!-- 添加图书的卡片 -->
        <div class="card text-white bg-secondary sticky-top">
          <div class="card-header">添加新图书</div>
          <form class="card-body bg-light" id="addForm">
            <!-- 书名 -->
            <div class="input-group mb-3">
              <div class="input-group-prepend">
                <span class="input-group-text">书名</span>
              </div>
              <input type="text" class="form-control" placeholder="请输入图书名称" name="bookname">
            </div>
            <!-- 作者 -->
            <div class="input-group mb-3">
              <div class="input-group-prepend">
                <span class="input-group-text">作者</span>
              </div>
              <input type="text" class="form-control" placeholder="请输入作者名字" name="author">
            </div>
            <!-- 出版社 -->
            <div class="input-group mb-3">
              <div class="input-group-prepend">
                <span class="input-group-text">出版社</span>
              </div>
              <input type="text" class="form-control" placeholder="请输入出版社名称" name="publisher">
            </div>
            <!-- 添加按钮 -->
            <button class="btn btn-dark" type="submit">添加</button>
          </form>
        </div>
      </div>
    </div>
  </div>

  <!-- 引入 lib 目录下的 jQuery 和 axios -->
  <script src="./lib/jquery-v3.6.0.js"></script>
  <script src="./lib/axios.js"></script>
  <script>
    axios.defaults.baseURL = "http://www.liulongbin.top:3006"
  </script>
  <script>
    $(function () {
      renderBookList();

      // 渲染到页面
      function renderBookList() {
        axios.get("/api/getbooks").then(({
          data: res
        }) => {
          console.log(res);
          let arr = []
          res.data.forEach(ele => {
            arr.push(`<tr>
              <th scope="row">${ele.id}</th>
              <td>${ele.bookname}</td>
              <td>${ele.author}</td>
              <td>${ele.publisher}</td>
              <td>
                <button  data-id="${ele.id}"  type="button" class="btn btn-link btn-sm">删除</button>
              </td>
            </tr>`)

          });
          $("tbody").empty().append(arr)
        })
      }



      // 添加图书事假绑定
      $("form").on("submit", function (e) {
        e.preventDefault();
        if ($("[name=bookname]").val().trim() == "") {
          $("[name=bookname]").val("")
          return alert("书名不能为空")
        }

        if ($("[name=author]").val().trim() == "") {
          $("[name=author]").val("")
          return alert("作者不能为空")
        }

        if ($("[name=publisher]").val().trim() == "") {
          $("[name=publisher]").val("")
          return alert("出版社不能为空")
        }

        // 发送axios添加图书
        axios.post("/api/addbook", $("form").serialize()).then(({
          data: res
        }) => {
          if (res.code != 201) {
            return alert(res.msg)
          }
          renderBookList();
          alert("恭喜添加成功成功")
          console.log(res);

          $("form")[0].rest();
        })

      })

      //  删除图书
      $("tbody").on("click", ".btn", function () {
        let id = $(this).attr("data-id")


        //  发送axios删除图书
        axios.get("/api/delbook", {
          params: {
            id: id
          }
        }).then(({
          data: res
        }) => {
          console.log(res.msg);
        })
      })

    })
  </script>
</body>

</html>